import {
	objDeepCopy
} from "@/assets/js/common.js";
let Controller = {
	methods: {
		//组装分页
		paginationFunction(that, obj) {
			that.Model.paginationObject.currentPage = obj.pageIndex; // 第几页
			that.Model.paginationObject.pageSize = obj.pageSize; //  每页数量
			that.Model.paginationObject.pageTotal = obj.pageTotal; // 页总数
			that.Model.paginationObject.total = obj.total; //总数
			that.Model.paginationObject.num1 = (obj.pageIndex - 1) * obj.pageSize + 1 //num1
			//num2
			if (obj.pageTotal == obj.pageIndex) {
				that.Model.paginationObject.num2 = obj.total;
			} else {
				that.Model.paginationObject.num2 = obj.pageIndex * obj.pageSize;
			}
		},
		//详情页tab切换
		tabsHandleClick(val) {
			this.Model.activeName = val;
			this.getTable(val);
		},
		getTable(activeName, pageSize, pageIndex) {
			switch (activeName) {
				case 'material':
					console.log(this.Model.styleNumber)
					this.Model.tableHeaderData = this.Model.materialTableHeader;
					this.getMaterial(this, this.Model.styleNumber, pageSize, pageIndex);
					break;
				case 'insert':
					this.Model.tableHeaderData = this.Model.insertTableHeader;
					this.getScoopRange(this, this.Model.styleNumber, pageSize, pageIndex);
					break;
				case 'handRange':
					this.Model.tableHeaderData = this.Model.handRangeTableHeader
					this.getHandRang(this, this.Model.styleNumber, pageSize, pageIndex);
					break;
				default:
					break;
			}
		},
		//获取材质
		getMaterial(that, styleNumber, pageSize, pageIndex) {
			var apiTest = this.apiTest;
			this.$ajax({
				method: 'post',
				url: apiTest + '/sale/saleStyleTrayMaterial/list',
				// url:'/api/sale/saleStyleTrayMaterial/list',
				data: {
					condition: {
						styleNumber: styleNumber
					},
					pageSize: pageSize,
					pageIndex: pageIndex
				}
			}).then((res) => {
				that.Model.tableData = [];
				console.log(res);
				var data = res.data;
				if (data.status == 200) {
					var obj = {}
					that.paginationFunction(that, data.result);
					var result = data.result.result;
					for (var i = 0; i < result.length; i++) {
						obj = objDeepCopy(result[i]);
						obj.styleName = that.Model.row.modelName;
						that.Model.tableData.push(obj);
					}
				} else {

				}
			});
		},
		//获取手寸
		getHandRang(that, styleNumber, pageSize, pageIndex) {
			var apiTest = this.apiTest;
			this.$ajax({
				method: 'post',
				url: apiTest + '/sale/saleStyleHandRange/list',
				// url:'/api/saleStyleHandRange/list',
				data: {
					condition: {
						styleNumber: styleNumber
					},
					pageSize: pageSize,
					pageIndex: pageIndex
				}
			}).then((res) => {
				that.Model.tableData = [];
				console.log(res);
				var data = res.data;
				if (data.status == 200) {
					var obj = {}
					var result = data.result.result;
					for (var i = 0; i < result.length; i++) {
						obj = objDeepCopy(result[i]);
						obj.styleName = that.Model.row.modelName;
						obj.handRange = obj.fromTheHand + '~' + obj.hand;
						that.Model.tableData.push(obj)
					}
				} else {

				}

			});
		},
		//获取镶口
		getScoopRange(that, styleNumber, pageSize, pageIndex) {
			var apiTest = this.apiTest;
			this.$ajax({
				method: 'post',
				// url:'/api/saleStyleInlaiedScoopRange/list',
				url: apiTest + '/sale/saleStyleInlaiedScoopRange/list',
				data: {
					condition: {
						styleNumber: styleNumber
					},
					pageSize: pageSize,
					pageIndex: pageIndex
				}
			}).then((res) => {
				that.Model.tableData = [];
				console.log(res);
				var data = res.data;
				if (data.status == 200) {
					var obj = {}
					var result = data.result.result;
					for (var i = 0; i < result.length; i++) {
						obj = objDeepCopy(result[i]);
						obj.styleName = that.Model.row.modelName;
						obj.orificeRange = obj.fromTheRabbet + '~' + obj.theRabbet;
						that.Model.tableData.push(obj);
					}
				} else {

				}

			});
		},
		backStyleInfo() {
			this.$router.push({
				path: 'styleInfo'
			})
		},
		//选择图片上传
		upfileChange(el, row) {
			var fileObj = el.target.files[0];
			var fileSize = fileObj.size;
			if (fileSize / 1024 / 1024 > 10) {
				this.tips(this, 'warning', '文件大小不能大于10M');
				return;
			} else {
				var form = new FormData();
				form.append("file", fileObj);
				form.append("fileType", 'IMG');
				this.uploadImg(this, form, row);
			}

		},
		//上传图片请求
		uploadImg(that, formData, row) {
			var apiTest = this.apiTest;
			this.$ajax({
				method: 'post',
				url: apiTest + '/base/file/uploadOne',
				// url:'/api/base/file/uploadOne',
				data: formData
			}).then((res) => {
				console.log(res);
				var data = res.data;
				if (data.status == 200) {
					//上传图片到服务器成功 调用替换接口 修改图片
					if (data.result) {
						var prams = {
							id: data.result.id,
							/*fileDir:data.result.fileDir,
							filePath:data.result.filePath,
							fileSize:data.result.fileSize,
							originalName:data.result.originalName,
							url:data.result.url,*/
						}
						that.changeImg(that, prams, row.id);
					}
				} else {
					that.tips(that, 'warning', data.msg);
				}
			});
		},
		//修改图片
		changeImg(that, prams, id) {
			var apiTest = this.apiTest;
			this.$ajax({
				method: 'post',
				url: apiTest + '/sale/saleStyleTrayMaterial/upLoadPicture/' + id,
				// url:'/api/sale/saleStyleTrayMaterial/upLoadPicture/'+id,
				data: prams
			}).then((res) => {
				console.log(res);
				var data = res.data;
				if (data.status == 200) {
					that.tips(that, 'success', '上传图片成功');
					//成功后更新列表
					that.getMaterial(that, that.Model.styleNumber);
				} else {
					that.tips(that, 'warning', data.msg);
				}
			});
		},
		//删除图片
		deleteImg(row) {
			this.Model.dialogAlert = true;
			this.Model.deleteImgId = row.id;
		},
		// 确认删除
		confirmDeleteImg() {
			var imgId = this.Model.deleteImgId;
			var apiTest = this.apiTest;
			var that = this;
			this.$ajax({
				method: 'get',
				url: apiTest + '/sale/saleStyleTrayMaterial/deletePicture/' + imgId,
			}).then((res) => {
				console.log(res);
				var data = res.data;
				if (data.status == 200) {
					that.tips(that, 'success', '删除图片成功');
					//成功后更新列表
					that.getMaterial(that, that.Model.styleNumber);
					setTimeout(function() {
						that.Model.dialogAlert = false;
					}, 1000);
				} else {
					that.tips(that, 'warning', data.msg);
				}
			});
		},
		handleSizeChange(val) {
			var activeName = this.Model.activeName;
			this.getTable(activeName, val);
		},
		handleCurrentChange(val) {
			var activeName = this.Model.activeName;
			this.getTable(activeName, 10, val);
		},
		// 
		showChild(val) {
			console.log(val);
		},
		toSearch(val) { //查询
			console.log(val);
		},
		toClear(val) {
			console.log(val);
		},
		toExport(val) {
			console.log(val);
		},
		tips(that, type, message) {
			that.$message({
				message: message,
				type: type
			});
		}
	},
	watch: {
		//
		$route: {
			handler(newValue, oldValue) {
				if (newValue.query.activeName) {
					var row = newValue.query.row;
					row = JSON.parse(row);
					this.Model.activeName = newValue.query.activeName;
					this.Model.styleNumber = newValue.query.styleNumber;
					this.Model.row = row;
					this.getTable(newValue.query.activeName);

				}
			},
			deep: true
		},
	}
}
export default Controller;
